class Wolf {
    constructor() {
        this.initData();
        this.Img();
    }
    initData() {
        //设置狼的状态
        this.statenomal = 0;
        this.stateblow = this.statenomal + 1;
        this.state = this.statenomal;

        this.wolfP = [
            { x: 170, y: 245 },
            { x: 50, y: 320 },
            { x: 45, y: 420 },
            { x: 70, y: 540 },
            { x: 200, y: 506 },
            { x: 330, y: 545 },
            { x: 320, y: 405 },
            { x: 305, y: 290 },
            { x: 200, y: 510 }
        ];

        this.Imgs = [
            [],
            []
        ];
        this.imgIndex = -1;
        this.WolfW = 108;
        this.WolfH = 101;
        this.pIndex = Math.floor(Math.random() * this.wolfP.length);
        this.Type = Math.floor(Math.random() * this.Imgs.length);
        this.endIndex = 6;
    }
    Img() {

        let imglist1 = [
            './img/wolf/h0.png',
            './img/wolf/h1.png',
            './img/wolf/h2.png',
            './img/wolf/h3.png',
            './img/wolf/h4.png',
            './img/wolf/h5.png',
            './img/wolf/h6.png',
            './img/wolf/h7.png',
            './img/wolf/h8.png',
            './img/wolf/h9.png'
        ];

        for (let i = 0; i < imglist1.length; i++) {
            this.Imgs[0][i] = new Image();
            this.Imgs[0][i].src = imglist1[i];
        }
        let imglist2 = [
            './img/wolf/x0.png',
            './img/wolf/x1.png',
            './img/wolf/x2.png',
            './img/wolf/x3.png',
            './img/wolf/x4.png',
            './img/wolf/x5.png',
            './img/wolf/x6.png',
            './img/wolf/x7.png',
            './img/wolf/x8.png',
            './img/wolf/x9.png'
        ];
        for (let i = 0; i < imglist1.length; i++) {
            this.Imgs[1][i] = new Image();
            this.Imgs[1][i].src = imglist2[i];
        }
    }
    logic() {
        this.imgIndex++;
        switch (this.state) {
            case this.statenomal:
                if (this.imgIndex == this.endIndex) {
                    this.imgIndex = 0;
                    this.pIndex = Math.floor(Math.random() * this.wolfP.length);
                    this.Type = Math.floor(Math.random() * this.Imgs.length);
                }
                break;
            case this.stateblow:
                if (this.imgIndex == this.endIndex) {
                    this.imgIndex = 0;

                    this.state = this.statenomal;
                    this.endIndex = 6;
                    this.pIndex = Math.floor(Math.random() * this.wolfP.length);
                    this.Type = Math.floor(Math.random() * this.Imgs.length);
                }
                break;
        }

    }
    run(paint) {
        this.logic();
        this.onDraw(paint);
    }
    onDraw(paint) {
        paint.drawImage(this.Imgs[this.Type][this.imgIndex], this.wolfP[this.pIndex].x, this.wolfP[this.pIndex].y, this.WolfW, this.WolfH);
    }
    onmousedown(x, y) {
        if (
            x >= this.wolfP[this.pIndex].x &&
            x <= (this.wolfP[this.pIndex].x + this.WolfW) &&
            y >= this.wolfP[this.pIndex].y &&
            y <= (this.wolfP[this.pIndex].y + this.WolfH) &&
            this.state != this.stateblow
        ) {
            this.state = this.stateblow;
            this.imgIndex = 6;
            this.endIndex = 10;
            this.grade += 10;
            this.onclick(this.Type)
        }
    }
}